package com.cssw.fyzb.tps.api.im;

import com.cssw.fyzb.tps.service.im.GroupService;
import com.cssw.fyzb.tps.service.im.common.ImHttpComm;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Jian Qin.
 */
@Function(description = "获取群组列表", parameters = {
        @Parameter(name = "limit", type = Type.Integer, description = "本次获取的群组 ID 数量的上限", required = false),
        @Parameter(name = "next", type = Type.Integer, required = false, description = "群太多时分页拉取标志，第一次填0，以后填上一次返回的值，返回的 Next 为0代表拉完了"),
        @Parameter(name = "groupType", type = Type.String, description = "返回特定群组形态的群组", required = false),
})
@Component
public class GetGroupList implements Action {
    @Autowired
    private GroupService groupService;

    @Override
    public Response execute(Request request) throws Exception {
        ImHttpComm.ImResult result = groupService.getGroupList(
                request.getInteger("limit"),
                request.getInteger("next"),
                request.getString("groupType"));
        if(result.getErrorCode() == 0){
            return Response.success()
                    .put("totalCount", result.getInteger("TotalCount"))
                    .put("next", result.getInteger("Next"))
                    .put("groupIdList", result.getList("GroupIdList"));
        }else{
            return Response.fail(result.getErrorInfo(), result.getErrorCode());
        }
    }
}
